﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess.SmartHouseDataSetTableAdapters;
using Common;

namespace DataAccess
{
    public class DataAccessor
    {
        private SmartHouseDataSet houseDataSet;

        //private SmartHouseDataSetTableAdapters.TableAdapterManager manager; // this is a question
        private BindingsTableAdapter bindingsTableAdapter;
        private ConditionsTableAdapter conditionsTableAdapter;
        private ActionTableAdapter actionAdapter;
        private EventsTableAdapter eventsTableAdapter;
        private OperationsTableAdapter operationsTableAdapter;
        private VariablesTableAdapter variablesTableAdapter;
        private EventWithVariableTableAdapter eventWithVariableAdapter;
        private EventWithConditionTableAdapter eventWithConditionAdapter;
        private OperationWithActionTableAdapter operationWithActionAdapter;

        public DataAccessor()
        {
            houseDataSet = new SmartHouseDataSet();
            //manager = new TableAdapterManager();
            
            bindingsTableAdapter = new BindingsTableAdapter();
            conditionsTableAdapter = new ConditionsTableAdapter();
            actionAdapter = new ActionTableAdapter();
            eventsTableAdapter = new EventsTableAdapter();
            operationsTableAdapter = new OperationsTableAdapter();
            operationWithActionAdapter = new OperationWithActionTableAdapter();
            variablesTableAdapter = new VariablesTableAdapter();
            eventWithVariableAdapter = new EventWithVariableTableAdapter();
            eventWithConditionAdapter = new EventWithConditionTableAdapter();
        }

        public SmartHouseDataSet Get()
        {
            variablesTableAdapter.Fill(houseDataSet.Variables);
            conditionsTableAdapter.Fill(houseDataSet.Conditions);
            eventsTableAdapter.Fill(houseDataSet.Events);
            eventWithVariableAdapter.Fill(houseDataSet.EventWithVariable);
            eventWithConditionAdapter.Fill(houseDataSet.EventWithCondition);

            actionAdapter.Fill(houseDataSet.Action);
            operationsTableAdapter.Fill(houseDataSet.Operations);
            operationWithActionAdapter.Fill(houseDataSet.OperationWithAction);
            
            bindingsTableAdapter.Fill(houseDataSet.Bindings);


            return houseDataSet;
        }

        public void Save(SmartHouseDataSet SHouseDataSet)
        {
            houseDataSet = SHouseDataSet;

            variablesTableAdapter.Update(houseDataSet);
            conditionsTableAdapter.Update(houseDataSet);
            eventsTableAdapter.Update(houseDataSet);
            eventWithVariableAdapter.Update(houseDataSet);
            eventWithConditionAdapter.Update(houseDataSet);

            actionAdapter.Update(houseDataSet);
            operationsTableAdapter.Update(houseDataSet);
            operationWithActionAdapter.Update(houseDataSet);

            bindingsTableAdapter.Update(houseDataSet);
        }

        /*public void WriteAllVarialbes(List<IVariable> variables)
        {
            foreach(IVariable variabl in variables)
            {
                houseDataSet.Variables.AddVariablesRow(variabl.Name, variabl.Discription, variabl.CurrentValue, variabl.Type);
            }
            variablesTableAdapter.Update(houseDataSet);
        }*/
    }
}
